package com.acoffee.java.DynamicProxy;

import net.sf.cglib.proxy.Enhancer;
import net.sf.cglib.proxy.InvocationHandler;
import net.sf.cglib.proxy.MethodInterceptor;
import net.sf.cglib.proxy.MethodProxy;

import java.lang.reflect.Method;

/**
 * @author acoffee
 * @create 2021-11-15 14:30
 */
public class CglibDynamic {
//    public Object getProxyObject(Object targetClass) {
//        Object obj = Enhancer.create(targetClass.getClass(), targetClass.getClass().getInterfaces(), new MethodInterceptor() {
//            @Override
//            public Object intercept(Object obj, Method method, Object[] args, MethodProxy proxy) throws Throwable {
//
//                return null;
//            }
//        });
//        return obj;
//    }


    public Object getProxyObject(Object targetClass) {
        Object obj = Enhancer.create(targetClass.getClass(),new InvocationHandler() {
            @Override
            public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
                LogService logService = new LogService();
                logService.beforePrint();
                Object invoke = method.invoke(targetClass, args);
                logService.afterPrint();
                return invoke;
            }
        });
        return obj;
    }
//    public Object getProxyObject(Object targetClass) {
//        Object obj = Enhancer.create(targetClass.getClass(),new InvocationHandler() {
//            @Override
//            public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
//                LogService logService = new LogService();
//                logService.beforePrint();
//                Object invoke = method.invoke(targetClass, args);
//                logService.afterPrint();
//                return invoke;
//            }
//        });
//        return obj;
//    }

}
